library(foreign)
library(iotools)

#This script uses the output from eba_pth_A.py

#Set working directory
#setwd()
dir.path <- '/eba.out.pthA/'

deci <- function(x, k, center = TRUE) {
	a <- format(round(x, k), nsmall=k)
	return(a)
}

#----------------------------------------------------
#Figure B3
#----------------------------------------------------


ids.pth_0 <- read.csv.raw(paste(dir.path, 'ID_Refs_0.csv', sep = ''))
ids.pth_1 <- read.csv.raw(paste(dir.path, 'ID_Refs_1.csv', sep = ''))
ids.pth_2 <- read.csv.raw(paste(dir.path, 'ID_Refs_2.csv', sep = ''))
ids.pth_3 <- read.csv.raw(paste(dir.path, 'ID_Refs_3.csv', sep = ''))
ids.pth_4 <- read.csv.raw(paste(dir.path, 'ID_Refs_4.csv', sep = ''))
ids.pth_5 <- read.csv.raw(paste(dir.path, 'ID_Refs_5.csv', sep = ''))
ids <- rbind(ids.pth_0, ids.pth_1, ids.pth_2, ids.pth_3, ids.pth_4, ids.pth_5)

failed.pth_0 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_0_0.csv', sep = ''))
failed.pth_1 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_1_0.csv', sep = ''))
failed.pth_2 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_2_0.csv', sep = ''))
failed.pth_3 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_3_0.csv', sep = ''))
failed.pth_4 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_4_0.csv', sep = ''))
failed.pth_5 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_5_0.csv', sep = ''))
failed.pth_6 <- read.csv.raw(paste(dir.path, 'FailedCoupsEBA_6_0.csv', sep = ''))

failed.pth <- rbind(
	failed.pth_0,
	failed.pth_1,
	failed.pth_2,
	failed.pth_3,
	failed.pth_4,
	failed.pth_5,
	failed.pth_6
	)



#Remove objects to free memory
rm(failed.pth_0)
rm(failed.pth_1)
rm(failed.pth_2)
rm(failed.pth_3)
rm(failed.pth_4)
rm(failed.pth_5)
rm(failed.pth_6)

success.pth_0 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_0_0.csv', sep = ''))
success.pth_1 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_1_0.csv', sep = ''))
success.pth_2 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_2_0.csv', sep = ''))
success.pth_3 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_3_0.csv', sep = ''))
success.pth_4 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_4_0.csv', sep = ''))
success.pth_5 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_5_0.csv', sep = ''))
success.pth_6 <- read.csv.raw(paste(dir.path, 'SuccCoupsEBA_6_0.csv', sep = ''))

success.pth <- rbind(
	success.pth_0,
	success.pth_1,
	success.pth_2,
	success.pth_3,
	success.pth_4,
	success.pth_5,
	success.pth_6
	)

rm(success.pth_0)
rm(success.pth_1)
rm(success.pth_2)
rm(success.pth_3)
rm(success.pth_4)
rm(success.pth_5)
rm(success.pth_6)

pdf('FigureB3.pdf', width = 20, height = 5)
	par(mfrow=c(1,2))
	plot(density(na.omit(failed.pth$tVal), n = 2000, adjust = 4),
		xlim = c(-7, 7),
		main = '',
		lwd = 3,
		xaxt = "n",
		xlab = paste('t-values'),
		las = 1)
	abline(v = -2, lty = 2, col = 'grey')
	abline(v = 2, lty = 2, col = 'grey')
	abline(v = 0, lty = 1)
	axis(1, at = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10), label = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10))
	title(expression(paste('Failed Coups (', phi, ')')))

	plot(density(na.omit(success.pth$tVal), n = 2000, adjust = 4),
		xlim = c(-7, 7),
		main = '',
		lwd = 3,
		xaxt = "n",
		xlab = 't-values',
		las = 1)
	abline(v = -2, lty = 2, col = 'grey')
	abline(v = 2, lty = 2, col = 'grey')
	abline(v = 0, lty = 1)
	axis(1, at = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10), label = c(-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10))
	title(expression(paste('Successful Coups (', psi, ')')))
dev.off()




#----------------------------------------------------
#Table B2
#----------------------------------------------------
failed.pth <- subset(failed.pth, !is.na(pVal))
cf.shnegtot.fl.pth <- sum(failed.pth$Coef < 0)/nrow(failed.pth)
cf.shnegsig.fl.pth <- sum(failed.pth$Coef < 0 & failed.pth$pVal < 0.05)/nrow(failed.pth)
cf.shpostot.fl.pth <- sum(failed.pth$Coef > 0)/nrow(failed.pth)
cf.shpossig.fl.pth <- sum(failed.pth$Coef > 0 & failed.pth$pVal < 0.05)/nrow(failed.pth)
cf.average.fl.pth <- mean(failed.pth$Coef)
se.average.fl.pth <- mean(failed.pth$SE)
tv.average.fl.pth <- mean(failed.pth$tVal)

success.pth <- subset(success.pth, !is.na(pVal))
cf.shnegtot.sc.pth <- sum(success.pth$Coef < 0)/nrow(success.pth)
cf.shnegsig.sc.pth <- sum(success.pth$Coef < 0 & success.pth$pVal < 0.05)/nrow(success.pth)
cf.shpostot.sc.pth <- sum(success.pth$Coef > 0)/nrow(success.pth)
cf.shpossig.sc.pth <- sum(success.pth$Coef > 0 & success.pth$pVal < 0.05)/nrow(success.pth)
cf.average.sc.pth <- mean(success.pth$Coef)
se.average.sc.pth <- mean(success.pth$SE)
tv.average.sc.pth <- mean(success.pth$tVal)

eba.out.table.fl <- cbind(
	'Failed Coup (phi)',
	deci(cf.shnegtot.fl.pth, 3),
	deci(cf.shnegsig.fl.pth, 3),
	deci(cf.shpostot.fl.pth, 3),
	deci(cf.shpossig.fl.pth, 3),
	deci(cf.average.fl.pth, 2),
	deci(se.average.fl.pth, 2),
	deci(tv.average.fl.pth, 2)
	)
#
eba.out.table.sc <- cbind(
	'Successful Coup (psi)',
	deci(cf.shnegtot.sc.pth, 3),
	deci(cf.shnegsig.sc.pth, 3),
	deci(cf.shpostot.sc.pth, 3),
	deci(cf.shpossig.sc.pth, 3),
	deci(cf.average.sc.pth, 2),
	deci(se.average.sc.pth, 2),
	deci(tv.average.sc.pth, 2)
	)
#
the_header <- cbind(
	c('Variable', '', ''),
	c('Prop. of Coef.', 'Negative', ''),
	c('Prop. of Coef.', 'Negative and', 'p<0.05'),
	c('Prop. of Coef.', 'Positive', ''),
	c('Prop. of Coef.', 'Positive and', 'p<0.05'),
	c('Mean Coefficient', '', ''),
	c('Mean SE', '' ,''),
	c('Mean t-Value', '' ,'')
	)


library(xtable)
TableB2 <- print(
			 xtable(rbind(the_header, eba.out.table.fl, eba.out.table.sc),
			 align = 'l|l|cc|cc|ccc|',
			  label = 'ext:bnds'),
		 include.rownames = FALSE,
		 include.colnames = FALSE,
		 caption.placement = 'top',
		 table.placement = "H",
		 hline.after = c(0,0, 3, 3, 4, 5, 5),
		 floating = FALSE
		 )

